package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslShippingAddress;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.IBaseAreaService;
import com.cz.zslu.service.IZslShippingAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 收货地址管理 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/shippingAddress")
@Api(description = "收货地址管理")
public class ShippingAddressController extends BaseController<IZslShippingAddressService, ZslShippingAddress> {
    @Autowired
    private IBaseAreaService iBaseAreaService;

    @ApiOperation("获取全部收货地址分页")
    @RequestMapping(value = "/getAllAddressPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslShippingAddress>> getAllAddressPage(@ApiParam("每页条数") @RequestParam(defaultValue =
            "10") int limit,
                                                                    @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslShippingAddress> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        Page<ZslShippingAddress> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取当前登录收货地址分页")
    @RequestMapping(value = "/getMyAddressPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslShippingAddress>> getMyAddressPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10"
    ) int limit,
                                                                   @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslShippingAddress> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.orderBy("crt_time", false);
        Page<ZslShippingAddress> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslShippingAddress info : obj.getRecords()) {
            String area =
                    iBaseAreaService.selectById(info.getProvince()).getName() + iBaseAreaService.selectById(info.getCity()).getName() + iBaseAreaService.selectById(info.getDistrict()).getName();
            info.setArea(area);
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslShippingAddress> findInfoById(@ApiParam("id") @RequestParam Long id) {
        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveShippingAddress", method = RequestMethod.POST)
    public ReturnObject<ZslShippingAddress> saveShippingAddress(@ApiParam("名称") @RequestParam String name,
                                                                @ApiParam("电话") @RequestParam String phone,
                                                                @ApiParam("省") @RequestParam String province,
                                                                @ApiParam("市") @RequestParam String city,
                                                                @ApiParam("区") @RequestParam String district,
                                                                @ApiParam("详情地址") @RequestParam String address) {
        ZslShippingAddress info = new ZslShippingAddress();
        info.setName(name);
        info.setPhone(phone);
        info.setProvince(province);
        info.setCity(city);
        info.setDistrict(district);
        info.setAddress(address);
        info.setUserId(BaseContextHandler.getUserID());
        baseService.insert(info);

        return new ReturnObject<>(0, "新增成功", null);
    }


    @ApiOperation("编辑")
    @RequestMapping(value = "/editShippingAddress", method = RequestMethod.POST)
    public ReturnObject<ZslShippingAddress> editShippingAddress(@ApiParam("id") @RequestParam Long id,
                                                                @ApiParam("名称") @RequestParam String name,
                                                                @ApiParam("电话") @RequestParam String phone,
                                                                @ApiParam("省") @RequestParam String province,
                                                                @ApiParam("市") @RequestParam String city,
                                                                @ApiParam("区") @RequestParam String district,
                                                                @ApiParam("详情地址") @RequestParam String address) {
        ZslShippingAddress info = baseService.selectById(id);
        info.setName(name);
        info.setPhone(phone);
        info.setProvince(province);
        info.setCity(city);
        info.setDistrict(district);
        info.setAddress(address);
        info.setUserId(BaseContextHandler.getUserID());
        baseService.updateById(info);

        return new ReturnObject<>(0, "编辑成功", null);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public ReturnObject<ZslShippingAddress> delById(@ApiParam("id") @RequestParam Long id) {

        ZslShippingAddress info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }
}

